//
//  PagForGameOfMatch.m
//  dfdsf
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "PagForGameOfMatch.h"
#import "CellForMatch.h"
#import "NetworkHandle.h"
#import "ModelForMatch.h"
#import "IconDetailController.h"
#import "DataDetailsController.h"
#import <DKNightVersion.h>
#import "UIColor+Wonderful.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface PagForGameOfMatch ()<NetworkHandleDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *array;



@end

@implementation PagForGameOfMatch

-(void)dealloc {
    
    [_collectionView release];
    [_array release];
    [super dealloc];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"apper" object:nil];


}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   self.navigationItem.hidesBackButton = YES;
    [self handleData];
    [self creataCollectionView];

}

#pragma mark - 创建 collectionView
- (void)creataCollectionView {
    
    // 布局 collectionView 上面的 cell
    UICollectionViewFlowLayout *flawLayout = [[UICollectionViewFlowLayout alloc] init];

    flawLayout.minimumInteritemSpacing = 10 * HEIGHTT;
    flawLayout.minimumLineSpacing = 10 * WEIGHTT;
    
    flawLayout.sectionInset = UIEdgeInsetsMake(10 * WEIGHTT, 10 * HEIGHTT, 10 * WEIGHTT, 10 * HEIGHTT);
    flawLayout.itemSize = CGSizeMake((WIDTH - 50 * WEIGHTT) / 3 , (WIDTH - 50 * WEIGHTT) / 3);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flawLayout];
    
    [flawLayout release];
    
    self.collectionView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor grayColor]);
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    

    [self.collectionView registerClass:[CellForMatch class] forCellWithReuseIdentifier:@"poolForMatch"];
    
}

#pragma mark - 数据处理
- (void)handleData {
    
    NSString *str = @"http://interface.sina.cn/sports/sports_navs/client_sports_ctrl/client_sports_ctrl.d.json";
   
    [NetworkHandle handlerJSONWitURL:str delegate:self];
    
}

- (void)handleDidComplete:(id)result {
    
    self.array = [NSMutableArray array];
    NSDictionary *dic = [result objectForKey:@"result"];
    
    NSDictionary *dic2 = [dic objectForKey:@"data"];
    
    NSArray *arr = [dic2 objectForKey:@"list"];
    
    for (NSDictionary *dicT in arr) {
        ModelForMatch *model = [[ModelForMatch alloc] init];
        [model setValuesForKeysWithDictionary:dicT];
        
        [self.array addObject:model];
        
        [model release];
    }
    
    [self.collectionView reloadData];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.array.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CellForMatch *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForMatch" forIndexPath:indexPath];
   // cell.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
    [cell passModelForMacth:[self.array objectAtIndex:indexPath.item]];
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    IconDetailController *icon = [[IconDetailController alloc] init];
    
    ModelForMatch *model = [self.array objectAtIndex:indexPath.item];
    
  NSString *str = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=livecast&show_extra=1&f=livecast_id,LeagueType,status,Team1Id,Team2Id,Score1,Score2,MatchId,LiveMode,Discipline,data_from,Title,date,time,Team1,Team2,Flag1,Flag2,NewsUrl,VideoUrl,LiveUrl,LiveStatusExtra,VideoLiveUrl,VideoBeginTime,narrator,LeagueType_cn,Discipline_cn,comment_id,odds_id,VideoEndTime,if_rotate_video,LiveStatusExtra_cn,m3u8,android,period_cn,program,penalty1,media_tag,penalty1,Round_cn,extra,rec_ovx,VideoId&_sport_a_=typeMatches&l_type=";

    icon.strUrl = [str stringByAppendingFormat:@"%@", model.ID];
    
    
    icon.ID = model.ID;
    
     if ([model.ID isEqualToString:@"4"] || [model.ID isEqualToString:@"10"] || [model.ID isEqualToString:@"nba"] || [model.ID isEqualToString:@"cba_1"] || [model.ID isEqualToString:@"2"] || [model.ID isEqualToString:@"3"] || [model.ID isEqualToString:@"1"] || [model.ID isEqualToString:@"5"] || [model.ID isEqualToString:@"219"] || [model.ID isEqualToString:@"213"] || [model.ID isEqualToString:@"328"] || [model.ID isEqualToString:@"11"] || [model.ID isEqualToString:@"7"] || [model.ID isEqualToString:@"15"] || [model.ID isEqualToString:@"16"] || [model.ID isEqualToString:@"18"] || [model.ID isEqualToString:@"112"] || [model.ID isEqualToString:@"7_c"] || [model.ID isEqualToString:@"9_c"] || [model.ID isEqualToString:@"10_c"] || [model.ID isEqualToString:@"tennis,tennis_0,tennis_1,tennis_2,tennis_3,tennis_4,tennis_5"]) {
         
         
         [[NSNotificationCenter defaultCenter] postNotificationName:@"pleaseHide" object:nil];
        icon.name = model.title;
        [self.navigationController pushViewController:icon animated:YES];
        
        [icon release];
        
    }else {
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"暂时没有数据" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *alartOK = [UIAlertAction actionWithTitle:@"OK" style:0 handler:^(UIAlertAction * _Nonnull action) {
           
            
        }];
        [alert addAction:alartOK];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
